#!/bin/sh
# by hxinwei@gmail.com
# License: GNU General Public License 2 (GPL2)

if [ -n "$OCF_DEBUG_LIBRARY" ]; then
    . $OCF_DEBUG_LIBRARY
else
    : ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
fi

# Parameter defaults

OCF_RESKEY_scsi_reserve_default="/usr/sbin/scsi_reserve"
OCF_RESKEY_sharedisk_default="/dev/sdb"
OCF_RESKEY_start_loop_default="10"

: ${OCF_RESKEY_scsi_reserve=${OCF_RESKEY_scsi_reserve_default}}
: ${OCF_RESKEY_sharedisk=${OCF_RESKEY_sharedisk_default}}
: ${OCF_RESKEY_start_loop=${OCF_RESKEY_start_loop_default}}

scsi2reserve_meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="scsi2reservation" version="1.0">
<version>1.0</version>

<longdesc lang="en">
The scsi-2-reserve resource agent is a place holder for SCSI-2 reservation.
A healthy instance of scsi-2-reserve resource, indicates the own of the specified SCSI device.
This resource agent depends on the scsi_reserve from scsires package, which is Linux specific.
</longdesc>

<shortdesc lang="en">
scsi-2 reservation
</shortdesc>

<parameters>

<parameter name="scsi_reserve" unique="0" required="0">
<longdesc lang="en">
The scsi_reserve is a command from scsires package. 
It helps one to issue SCSI-2 reservation on SCSI devices.
</longdesc>
<shortdesc lang="en">Manages exclusive access to shared storage media thrugh SCSI-2 reservations</shortdesc>
<content type="string" default="${OCF_RESKEY_scsi_reserve_default}" />
</parameter>


<parameter name="sharedisk" unique="0" required="0">
<longdesc lang="en">
The shared disk that can be reserved.
</longdesc>
<shortdesc lang="en">
Shared disk.
</shortdesc>
<content type="string" default="${OCF_RESKEY_sharedisk_default}" />
</parameter>

<parameter name="start_loop" unique="0" required="0">
<longdesc lang="en">
We are going to try several times before giving up. Start_loop indicates how many times we are going to re-try.
</longdesc>
<shortdesc lang="en">
Times to re-try before giving up.
</shortdesc>
<content type="string" default="${OCF_RESKEY_start_loop_default}" />
</parameter>


</parameters>

<actions>
<action name="start"   timeout="300s" />
<action name="stop"    timeout="100s" />
<action name="monitor" depth="0"  timeout="20s" interval="20s" />
<action name="meta-data"  timeout="5s" />
<action name="validate-all"  timeout="30s" />
</actions>
</resource-agent>
END

	return $OCF_SUCCESS
}

scsi2reserve_usage() {
	cat <<END
		usage: $0 {start|stop|monitor|validate-all|meta-data}
END
}

case $__OCF_ACTION in
	meta-data)  scsi2reserve_meta_data
		exit $OCF_SUCCESS
		;;
	usage|help) scsi2reserve_usage
		exit $OCF_SUCCESS
		;;
	*)
		;;
esac		
	

scsi2reserve_validate ()
{
	if [ ! -x "${OCF_RESKEY_scsi_reserve}" ]; then
		exit $OCF_ERR_INSTALLED
	fi
	if [ -z "${OCF_RESKEY_sharedisk}" ]; then
		exit $OCF_ERR_GENERIC
	fi	
	if [ ! -b "${OCF_RESKEY_sharedisk}" ]; then
		exit $OCF_ERR_ARGS
	fi
}

scsi2reserve_validate

scsi2reserve_start ()
{
	ite=$OCF_RESKEY_start_loop
	while [ $ite -ge 0 ]; do	
		/sbin/startproc ${OCF_RESKEY_scsi_reserve} -d ${OCF_RESKEY_sharedisk} --preempt --hold --stonith
		sleep 15
		/sbin/checkproc ${OCF_RESKEY_scsi_reserve}
		rc=$?
		if [ $rc -eq 0 ]; then
			exit $OCF_SUCCESS
		fi		
		ite=`expr $ite - 1`
	done	
	exit $OCF_ERR_GENERIC
}

scsi2reserve_monitor ()
{
	/sbin/checkproc ${OCF_RESKEY_scsi_reserve}
	rc=$?
	if [ $rc -eq 0 ]; then
		exit $OCF_SUCCESS
	else
		exit $OCF_NOT_RUNNING
	fi	
}

scsi2reserve_stop ()
{
	/sbin/killproc ${OCF_RESKEY_scsi_reserve}
	${OCF_RESKEY_scsi_reserve} -d ${OCF_RESKEY_sharedisk} --release
	exit $OCF_SUCCESS
}

if [ $# -ne 1 ]; then
	scsi2reserve_usage
	exit $OCF_ERR_ARGS
fi

case $__OCF_ACTION in
	start)      scsi2reserve_start
		;;
	stop)       scsi2reserve_stop
		;;
	monitor)    scsi2reserve_monitor
		;;
	validate-all)   scsi2reserve_validate
			exit $OCF_SUCCESS
		;;
	*)      scsi2reserve_usage
		exit $OCF_ERR_UNIMPLEMENTED
	;;
esac

exit $?


